package os.kernelProcess.queues;

import java.util.LinkedList;

import os.MiniThread;

public class FCFSQueue extends MiniQueue
{
	protected LinkedList<MiniThread> queue = new LinkedList<MiniThread>();
	
	public void append(MiniThread thread)
	{
//		System.err.println("append");
		this.accessQueue.P();
//			System.err.println("koko lala 2");
			queue.addFirst(thread);
		this.accessQueue.V();
//		System.err.println("append out");
	}

	public MiniThread getHead()
	{
//		System.err.println("head");
		MiniThread result;
		this.accessQueue.P();
			result = queue.getLast();
		this.accessQueue.V();
//		System.err.println("head out");
		return result;
	}
	
	
	/**
	 * <b>UnImplemented</b>: Removes the thread of a given id from the queue.
	 * 
	 * @param tid
	 *            id of the thread to be removed.
	 * @return The removed thread.
	 */
	public MiniThread removeThread(String tid)
	{
//		System.err.println("remove");
		this.accessQueue.P();
			for (int i = 0, n = queue.size(); i < n; i++)
			{
				if (queue.get(i).getTid().equals(tid))
				{
					this.accessQueue.V();
//					System.err.println("remove out");
					return queue.remove(i);
				}
			}
		this.accessQueue.V();
//		System.err.println("remove out");
		return null;
	}
	
	/**
	 * <b>UnImplemented</b>: Gets the number of threads in the queue.
	 * 
	 * @return Number of threads in the queue.
	 */
	public int getSize()
	{
//		System.err.println("size");
		int size = 0;
		this.accessQueue.P();
			size = queue.size();
		this.accessQueue.V();
//		System.err.println("size out");
		return size;
	}
	

}
